<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="departmentName">
        <el-input v-model.trim="form.departmentName" />
      </el-form-item>
      <el-form-item label=" 备注" prop="remarks">
        <el-input type="textarea" v-model="form.remarks"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { insertDepartment, updateDepartment } from '@/api/wordOrder'

  export default {
    name: 'TableEdit',
    data() {
      return {
        form: {
          id: '',
          departmentName: '',
          remarks: '',
        },
        rules: {
          departmentName: [{ required: true, trigger: 'blur', message: '请输入部门名称' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '新增部门信息'
        } else {
          this.title = '编辑部门信息'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.$emit('refreshTable')
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              await updateDepartment(this.form)
            } else {
              await insertDepartment(this.form)
            }
            this.$baseMessage('提交成功', 'success')
            this.$refs['form'].resetFields()
            this.dialogFormVisible = false
            this.$emit('refreshTable')
            this.form = this.$options.data().form
          } else {
            return false
          }
        })
      },
    },
  }
</script>
